package com.ssplayer.utils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;

import com.ssplayer.bean.Music;
import com.ssplayer.bean.MusicList;
import com.ssplayer.bean.Video;
import com.ssplayer.database.DBHelper;

public class MediaUtils {

	/**
	 * 获取音乐列表
	 * 
	 * @param context
	 * @return
	 */
	public static List<Music> getMusics(Context context, int currentlist) {
		Cursor cursor;
		List<Music> musics = new ArrayList<Music>();
		if (currentlist == -1) {
			cursor = context.getContentResolver().query(
					MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
					null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		} else {
			DBHelper dbHelper = new DBHelper(context);
			List<String> musicidlist = dbHelper.getListMusics(currentlist);

			int size = musicidlist.size();
			StringBuilder selection = new StringBuilder(
					MediaStore.Audio.Media._ID + "=");
			for (int i = 0; i < size; i++) {
				if (i < size - 1) {
					selection.append("? or " + MediaStore.Audio.Media._ID + "=");
				} else {
					selection.append("?");
				}
			}
			String[] selectionArgs = (String[]) musicidlist
					.toArray(new String[size]);
			// 当音乐列表为空时给出一个查不出数据的条件
			if (size == 0) {
				selection.append("?");
				selectionArgs = new String[] { "-1" };
			}
			cursor = context.getContentResolver().query(
					MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
					selection.toString(), selectionArgs,
					MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		}

		while (cursor.moveToNext()) {
			long id = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id
			String title = cursor.getString((cursor
					.getColumnIndex(MediaStore.Audio.Media.TITLE))); // 音乐标题
			String artist = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.ARTIST)); // 艺术家
			String album = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.ALBUM)); // 专辑
			String displayName = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
			long albumId = cursor.getInt(cursor
					.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
			long duration = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Audio.Media.DURATION)); // 时长
			long size = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小
			String url = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径
			int isMusic = cursor.getInt(cursor
					.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 是否为音乐
			if (isMusic != 0) {
				Music music = new Music(id, title, artist, album, displayName,
						albumId, duration, size, url);
				musics.add(music);
			}
		}
		return musics;
	}

	public static List<MusicList> getMusicLists(Context context) {
		DBHelper dbHelper = new DBHelper(context);
		List<MusicList> musicLists = new ArrayList<MusicList>();
		Cursor cursor = dbHelper.getLists();
		while (cursor.moveToNext()) {
			long id = cursor.getLong(0); // 列表id
			String title = cursor.getString(1); // 列表标题
			MusicList music = new MusicList(id, title);
			musicLists.add(music);
		}
		return musicLists;
	}

	/**
	 * 获取视频列表
	 * 
	 * @param context
	 * @return
	 */
	public static List<Video> getVideos(Context context) {
		List<Video> videos = new ArrayList<Video>();
		Cursor cursor = context.getContentResolver().query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Video.Media.DEFAULT_SORT_ORDER);

		while (cursor.moveToNext()) {
			long id = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Video.Media._ID)); // 视频id
			String title = cursor.getString((cursor
					.getColumnIndex(MediaStore.Video.Media.TITLE))); // 视频标题

			long duration = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Video.Media.DURATION)); // 时长
			long size = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Video.Media.SIZE)); // 文件大小
			String url = cursor.getString(cursor
					.getColumnIndex(MediaStore.Video.Media.DATA)); // 文件路径
			// 查询缩略图
			String thumbnail = null;
			String selection = MediaStore.Video.Thumbnails.VIDEO_ID + "=?";
			String[] selectionArgs = { String.valueOf(id) };
			Cursor cursorThumbnail = context.getContentResolver().query(
					MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, null,
					selection, selectionArgs,
					MediaStore.Video.Thumbnails.DEFAULT_SORT_ORDER);
			if (cursorThumbnail.moveToFirst()) {
				thumbnail = cursorThumbnail.getString(cursorThumbnail
						.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
			}
			Video video = new Video(id, title, duration, size, url, thumbnail);
			videos.add(video);
		}
		return videos;
	}

	/**
	 * 格式化时间
	 * 
	 * @param time
	 * @return
	 */
	public static CharSequence formatDuration(long time) {
		String min = time / (1000 * 60) + "";
		String sec = time % (1000 * 60) + "";
		if (min.length() < 2) {
			min = "0" + time / (1000 * 60) + "";
		} else {
			min = time / (1000 * 60) + "";
		}
		if (sec.length() == 4) {
			sec = "0" + (time % (1000 * 60)) + "";
		} else if (sec.length() == 3) {
			sec = "00" + (time % (1000 * 60)) + "";
		} else if (sec.length() == 2) {
			sec = "000" + (time % (1000 * 60)) + "";
		} else if (sec.length() == 1) {
			sec = "0000" + (time % (1000 * 60)) + "";
		}
		return min + ":" + sec.trim().substring(0, 2);
	}

	/** 获取视频的缩略图 */
	public static Bitmap createVideoThumbnail(String filePath) {
		Bitmap bitmap = null;
		// 获取视频的缩略图
		bitmap = ThumbnailUtils.createVideoThumbnail(filePath,
				MediaStore.Images.Thumbnails.MICRO_KIND);
		bitmap = ThumbnailUtils.extractThumbnail(bitmap, 72, 56,
				ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
		return bitmap;
	}
}
